<?php

class openapi_ctl_admin_log_request extends desktop_controller {

    var $name = "请求日志";

    var $workground = "setting_tools";

    public function index() {
        if ($_GET['view'] == 0) {
            $actions[] = array(
                'label' => '重试',
                'submit' => 'index.php?app=openapi&ctl=admin_log_request&act=restart',
                'target' => "dialog::{width:800,height:400,title:'日志重试结果'}"
            );
        }
        $this->finder('openapi_mdl_request_log', array(
            'title' => '请求日志',
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => true,
            'use_buildin_export' => false,
            'use_buildin_import' => false,
            'use_buildin_filter' => true,
            'actions' => $actions
        ));
    }

    public function _views() {
        $request_log = $this->app->model('request_log');
        $sub_menu = array(
            0 => array(
                'label' => app::get('openapi')->_('失败'),
                'optional' => false,
                'filter' => array(
                    'status' => 'fail'
                )
            ),
            1 => array(
                'label' => app::get('openapi')->_('全部'),
                'optional' => false,
                'filter' => array(
                    'disabled' => 'false'
                )
            )
        );
        
        foreach ($sub_menu as $k => $v) {
            if ($v['optional'] == false) {
                $show_menu[$k] = $v;
                $show_menu[$k]['filter'] = $v['filter'] ? $v['filter'] : null;
                $show_menu[$k]['addon'] = $request_log->count($v['filter']);
                $show_menu[$k]['href'] = 'index.php?app=openapi&ctl=admin_log_request&act=index&view=' . ($k) . (isset($_GET['optional_view']) ? '&optional_view=' . $_GET['optional_view'] . '&view_from=dashboard' : '');
            } elseif (($_GET['view_from'] == 'dashboard') && $k == $_GET['view']) {
                $show_menu[$k] = $v;
            }
        }
        return $show_menu;
    }

    function restart() {
        $id = $_POST['log_id'];
        $isqueue = (count($id) > 10) ? true : false;
        $data = $this->app->model('request_log')->getList('*', array(
            'log_id|in' => $id
        ));
        $router = kernel::single('openapi_api_router');
        foreach ($data as &$value) {
            $value['res'] = $router->fialrequestctl($value, $isqueue);
        }
        $this->pagedata['res'] = $data;
        $this->display('admin/log/request/retry.html');
    }
}
?>
